const Koa = require("koa");
const app = new Koa();
const path = require("path");
const { bodyParser } = require("@koa/bodyparser");
const static = require("koa-static");
const Router = require("@koa/router");
const router = new Router();
// const bodyParser = require("./middlewares/bodyparser");

// 1.中间件可以扩展公共的逻辑
// 2.决定是否可以向下执行， 不调用next 就结束了  鉴权
// 3.给请求响应做一些属性扩展

// json , key=value
app.use(bodyParser());
// 默认用户访问一个页面，可以将页面展示出来， 静态服务端
// http://localhost:3000/index.html
app.use(static(path.resolve(__dirname, "public")));
app.use(static(path.resolve(__dirname, "static")));
app.use(static(path.resolve(__dirname, "assets")));

const multer = require("./middlewares/multer");
app.use(multer(path.resolve(__dirname, "upload")));
app.use(router.routes());
router.post("/login", function (ctx, next) {
  ctx.body = ctx.request.body;
  return next();
});
router.post("/login", function (ctx, next) {
  // console.log("页面跳转");

  console.log(ctx.request.body, ctx.request.file);

  ctx.redirect("/home.html");
});
app.use(function (ctx, next) {
  console.log("最终的next");
});
app.listen(3000);

// 可以保证异步请求处理的时的顺序
